package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.BatchQueryAchievementCopyrightOwningReq;
import com.sg.dto.base.app.req.QueryAchievementCopyrightOwningDetailReq;
import com.sg.dto.base.app.req.QueryAchievementCopyrightOwningListReq;
import com.sg.entity.OmsAchievementCopyrightOwning;
import com.sg.service.base.app.MOmsAchievementCopyrightOwningService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsAchievementCopyrightOwningMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAchievementCopyrightOwningServiceImpl
        extends ServiceImpl<OmsAchievementCopyrightOwningMapper, OmsAchievementCopyrightOwning>
        implements MOmsAchievementCopyrightOwningService {

    @Autowired
    OmsAchievementCopyrightOwningMapper omsAchievementCopyrightOwningMapper;

    /**
     * 3-10-07新增成果版权归属[9129]
     * gen by moon at 7/18/2024, 11:54:49 PM
     */
    @Trace(operationName = "3-10-07新增成果版权归属")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addAchievementCopyrightOwning(OmsAchievementCopyrightOwning omsAchievementCopyrightOwning) {
        omsAchievementCopyrightOwning.setAchievementCopyrightOwningId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAchievementCopyrightOwning.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAchievementCopyrightOwning.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementCopyrightOwning != null && omsAchievementCopyrightOwning.getSubjectLifeCycle() != null && omsAchievementCopyrightOwning.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementCopyrightOwning.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAchievementCopyrightOwning.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAchievementCopyrightOwning.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementCopyrightOwning != null && omsAchievementCopyrightOwning.getSubjectLifeCycle() != null && omsAchievementCopyrightOwning.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementCopyrightOwning.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAchievementCopyrightOwning.setIsValid("FALSE");
        if (super.save(omsAchievementCopyrightOwning)) {
            return omsAchievementCopyrightOwning.getAchievementCopyrightOwningId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-10-07修改成果版权归属[9130]
     * gen by moon at 4/4/2024, 10:03:11 AM
     */
    @Trace(operationName = "3-10-07修改成果版权归属")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAchievementCopyrightOwning(OmsAchievementCopyrightOwning omsAchievementCopyrightOwning) {
        omsAchievementCopyrightOwning.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAchievementCopyrightOwning != null && omsAchievementCopyrightOwning.getSubjectLifeCycle() != null && omsAchievementCopyrightOwning.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementCopyrightOwning.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAchievementCopyrightOwning != null && omsAchievementCopyrightOwning.getSubjectLifeCycle() != null && omsAchievementCopyrightOwning.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementCopyrightOwning.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAchievementCopyrightOwning.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAchievementCopyrightOwning != null && omsAchievementCopyrightOwning.getSubjectLifeCycle() != null && omsAchievementCopyrightOwning.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAchievementCopyrightOwning.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsAchievementCopyrightOwning != null && omsAchievementCopyrightOwning.getSubjectLifeCycle() != null && omsAchievementCopyrightOwning.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAchievementCopyrightOwning.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAchievementCopyrightOwning);
    }

    /**
     * 3-10-07查询成果版权归属详情[9131]
     * gen by moon at 4/4/2024, 10:03:15 AM
     */
    @Trace(operationName = "3-10-07查询成果版权归属详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAchievementCopyrightOwning queryAchievementCopyrightOwningDetail(QueryAchievementCopyrightOwningDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsAchievementCopyrightOwning> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAchievementCopyrightOwning> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAchievementCopyrightOwning retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-07批量删成果版权归属[9240]
     * gen by moon at 4/4/2024, 10:03:18 AM
     */
    @Trace(operationName = "3-10-07批量删成果版权归属")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteAchievementCopyrightOwning(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 3-10-07查询成果版权归属列表[9253]
     * gen by moon at 4/4/2024, 10:03:22 AM
     */
    @Trace(operationName = "3-10-07查询成果版权归属列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievementCopyrightOwning> queryAchievementCopyrightOwningList(QueryAchievementCopyrightOwningListReq reqDto) {

        try {
            QueryWrapper<OmsAchievementCopyrightOwning> queryWrapper = new QueryWrapper<>();


            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievementCopyrightOwning.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievementCopyrightOwning.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievementCopyrightOwning.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAchievementCopyrightOwning>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-10-07删除成果版权归属[9215]
     * gen by moon at 4/5/2024, 8:58:36 PM
     */
    @Trace(operationName = "3-10-07删除成果版权归属")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteAchievementCopyrightOwning(String achievementCopyrightOwningId) {

        return super.removeById(achievementCopyrightOwningId);
    }

    /**
     * 3-10-07批量查询成果版权归属[9330]
     * gen by moon at 4/7/2024, 8:49:30 AM
     */
    @Trace(operationName = "3-10-07批量查询成果版权归属")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAchievementCopyrightOwning> batchQueryAchievementCopyrightOwning(BatchQueryAchievementCopyrightOwningReq reqDto) {

        try {
            QueryWrapper<OmsAchievementCopyrightOwning> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getAchievementList() == null || reqDto.getAchievementList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("achievement_id", reqDto.getAchievementList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAchievementCopyrightOwning.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAchievementCopyrightOwning.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAchievementCopyrightOwning.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsAchievementCopyrightOwning>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
